# -*- coding:utf-8 -*-
import pymysql
from flask import Blueprint, request

from apps import exception, result
from apps.views import api_decorator

bp = Blueprint('user', __name__, url_prefix='/user')


@bp.route('/info', methods=['POST'])
@api_decorator
def userInfo():
    content = request.json or {}
    conn = pymysql.connect(host="localhost", user="root", password="123456", db="little_pig", port=3306,
                           charset='utf8')
    client = conn.cursor(cursor=pymysql.cursors.DictCursor)

    nickname = content.get('nickname', '')
    city = content.get('city', '')
    country = content.get('country', '')
    gender = content.get('gender', '')
    language = content.get('language', '')
    province = content.get('province', '')
    avatarUrl = content.get('avatarUrl', '')
    position = content.get('position', '')

    sql = 'insert into user values("{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}")'

    code = client.execute(sql.format(nickname, city, country, gender, language, province, avatarUrl, position))
    if not code:
        raise exception.SQLException()
    conn.commit()

    data = {
        'nickname': nickname,
        'city': city,
        'country': country,
        'gender': gender,
        'language': language,
        'province': province,
        'avatarUrl': avatarUrl,
    }
    return result.from_data(data)

